package com.cn.springcloudwechat.serviceImpl.vaccinstation;

import com.cn.springcloudwechat.common.mybatis.Page;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.mapper.mapper.vaccinestation.YmStationVMapper;
import com.cn.springcloudwechat.mapper.mapper.vaccinestation.YmStationWorkOrderVMapper;
import com.cn.springcloudwechat.mapper.mapper.vaccinestation.YmStationWorkTimeVMapper;
import com.cn.springcloudwechat.mapper.mapper.vaccinestation.YmStationWorkVMapper;
import com.cn.springcloudwechat.model.YmUser;
import com.cn.springcloudwechat.model.vaccinestion.YmStationVWork;
import com.cn.springcloudwechat.model.vaccinestion.YmStationWorkVOrder;
import com.cn.springcloudwechat.model.vaccinestion.YmStationWorkVTime;
import com.cn.springcloudwechat.model.vaccinestion.YmVStation;
import com.cn.springcloudwechat.service.vaccinstation.VaccineStationService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("VaccineStationService")
public class VaccineStationServiceImpl implements VaccineStationService {

	@Autowired
	YmStationVMapper ymStationVMapper;
	@Autowired
	YmStationWorkVMapper ymStationWorkVMapper;
	@Autowired
	YmStationWorkTimeVMapper ymStationWorkTimeVMapper;
	@Autowired
	YmStationWorkOrderVMapper ymStationWorkOrderVMapper;

	/**
	 * 疫苗站新增
	 * @param ymVStation
	 * @param YmStationWorkVTime
	 */
	@Override
	public void insertVaccineStationSer(YmVStation ymVStation, YmStationWorkVTime YmStationWorkVTime) {
		//新增疫苗站主信息(ym_station)
		ymStationVMapper.insertSelective(ymVStation);
		//新增疫苗站工作时间站主信息(ym_station_work)
		/*String[] workDates = ymVStation.getWorkTime().split(",");
		YmStationVWork YmStationVWork = new YmStationVWork();
		for(String workDate : workDates){
		YmStationVWork.setStationId(ymVStation.getStationId());
		YmStationVWork.setWorkDate(Integer.parseInt(workDate));
		ymStationWorkVMapper.insertSelective(YmStationVWork);
	    }
		String[] times = YmStationWorkVTime.getTime().split(",");
		//上班时间段(ym_station_work_time)
		for(String time : times){
			String[] tm = time.split("/");
			YmStationWorkVTime.setAmpm(tm[0]);
			YmStationWorkVTime.setTime(tm[1]);
			ymStationWorkTimeVMapper.insertSelective(YmStationWorkVTime);
		}*/
	}

	/**
	 * 疫苗站读取
	 * @param ymVStation
	 * @param page
	 * @return
	 */
	@Override
	public Page<YmVStation> queryVaccineStationSer(YmVStation ymVStation, Page<YmVStation> page) {
		/*PageHelper.startPage(page.getPageNum(),page.getPageSize());*/
		page.setResults(ymStationVMapper.selectByPrimaryKey(ymVStation,page));
		return page;
	}

	/**
	 * 疫苗站疫苗详情的新增(包括时间段和对应的疫苗)
	 * @param ymStationWorkVOrder
	 * @param parameter
	 */
	@Override
	public void insertVaccineStationDetailSer(YmStationWorkVOrder ymStationWorkVOrder, String[] parameter) {
		ymStationWorkVOrder.getStationId();
		ymStationWorkVOrder.getVaccineId();
		for(String pt : parameter){
		ymStationWorkVOrder.setWorkTimeDescId(Long.parseLong(pt));
		ymStationWorkOrderVMapper.insertSelective(ymStationWorkVOrder);
		}
	}

	/**
	 * 查询某一个疫苗站疫苗(包括时间段和对应的疫苗)
	 * @param ymVStation
	 * @return
	 */
	@Override
	public List<YmVStation> queryVaccineStationDetailSer(YmVStation ymVStation) {
		return ymStationVMapper.queryVaccineStationDetailMapper(ymVStation);
	}

	/**
	 * 查询客户疫苗站
	 * @param ajaxRes
	 * @return
	 */
	@Override
	public Page<YmVStation> queryVaccineUserSer(String stationName,String address,Page<YmVStation> page) {
//		YmUser ymUser = (YmUser) ThreadLocalUtil.getUser();
//		String address = ymUser.getAddress();
		//查出所有疫苗站的地址(所在市)
		List<YmVStation> ymVStations = ymStationVMapper.queryVaccineUserMapper(stationName,address,page);
		page.setResults(ymVStations);
		return page;
		
	}

	/**
	 * 疫苗站修改
	 * @param ajaxRes
	 * @param ymVStation
	 * @param YmStationWorkVTime
	 * @return
	 */
	@Override
	public void updateVaccineStationSer(YmVStation ymVStation,YmStationWorkVTime YmStationWorkVTime) {
		//新增疫苗站主信息(ym_station)
				ymStationVMapper.updateByPrimaryKeySelective(ymVStation);
				//新增疫苗站工作时间站主信息(ym_station_work)
				/*ymStationWorkVMapper.deleteByPrimaryKeyOther(ymVStation.getStationId());
				ymStationWorkTimeVMapper.deleteByPrimaryKeyOther(ymVStation.getStationId());
				String[] workDates = ymVStation.getWorkTime().split(",");
				YmStationVWork YmStationVWork = new YmStationVWork();
				for(String workDate : workDates){
				YmStationVWork.setStationId(ymVStation.getStationId());
				YmStationVWork.setWorkDate(Integer.parseInt(workDate));
				ymStationWorkVMapper.insertSelective(YmStationVWork);
			    }
				String[] times = YmStationWorkVTime.getTime().split(",");
				//上班时间段(ym_station_work_time)
				for(String time : times){
					String[] tm = time.split("/");
					YmStationWorkVTime.setAmpm(tm[0]);
					YmStationWorkVTime.setTime(tm[1]);
					ymStationWorkTimeVMapper.insertSelective(YmStationWorkVTime);
				}*/
	}


	/**
	 * 查询地区所有疫苗站
	 * @param ymVStation
	 * @param page
	 * @return
	 */
	@Override
	public List<YmVStation> queryVaccineStationByAreaId(YmVStation ymVStation, Page<YmVStation> page) {
		List<YmVStation> ymVStations = ymStationVMapper.queryVaccineStationByAreaId(ymVStation, page);
		return ymVStations;
	}
}
